package com.xmotor.test_swagger_spring_boot_starter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description 测试swagger组件controller
 * @Author Dang Xianyue
 * @Date 2020-08-15 10:47
 * @Version 1.0
 */
@Slf4j
@Api(tags = "测试swagger-spring-boot-starter接口")
@RestController
@RequestMapping("/testSwaggerSpringBootStarter")
public class TestSwaggerSpringBootStarterController {

    @ApiOperation(value = "test1接口",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "编号",paramType = "form",dataType = "Integer",required = true),
            @ApiImplicitParam(name = "name",value = "名称",paramType = "form",dataType = "String",required = true),
            @ApiImplicitParam(name = "flag",value = "标记",paramType = "form",dataType = "Boolean",required = true)
    })
    @RequestMapping(value = "/test1",method = RequestMethod.POST)
    public Result test1(@RequestParam(value = "id")Integer id,
                   @RequestParam(value = "name")String name,
                   @RequestParam(value = "flag")Boolean flag){
        JSONObject jo = new JSONObject();
        jo.put("id",id);
        jo.put("name",name);
        jo.put("flag",flag);
        log.info("test1接口参数：{}", JSON.toJSONString(jo));
        return new Result(200,"请求成功!",jo);
    }

    @ApiOperation(value = "test2接口",httpMethod = "POST")
    @RequestMapping(value = "/test2",method = RequestMethod.POST)
    public Result test2(@RequestBody UserVO userVO, HttpServletRequest request) throws Exception{
        String token = request.getHeader("token");

        if (StringUtils.isBlank(token) || !"123456".equals(token)){
            return new Result(201,"没有权限！",null);
        }
        log.info("test2接口参数：{}", JSON.toJSONString(userVO));
        return new Result(200,"请求成功!",userVO);
    }
}
